package com.github.pig.admin.controller;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.mapper.Wrapper;
import com.github.pig.admin.model.entity.SysRegion;
import com.github.pig.admin.service.sys.ISysRegionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.github.pig.common.constant.CommonConstant;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.github.pig.common.util.Query;
import com.github.pig.common.util.R;
import com.github.pig.common.web.BaseController;

/**
 * 区域信息 四级联动
 *
 * @author sxf
 * @since 2018-12-22
 */
@RestController
@RequestMapping("/sysRegion")
public class SysRegionController extends BaseController {
    @Autowired private ISysRegionService sysRegionService;
    /**
     * 通过ID查询
     *
     * @param pId 父节点
     * @return SysRegion
     */
    @GetMapping("/getByParentId/{pId}")
    public R<List<SysRegion>> getByParentId(@PathVariable Integer pId) {
        Wrapper wrapper = new EntityWrapper();
        wrapper.eq("pid",pId);
        return new R<>(sysRegionService.selectList(wrapper));
    }
    /**
    * 通过ID查询
    *
    * @param id ID
    * @return SysRegion
    */
    @GetMapping("/{id}")
    public R<SysRegion> get(@PathVariable Integer id) {
        return new R<>(sysRegionService.selectById(id));
    }


    /**
    * 分页查询信息
    *
    * @param params 分页对象
    * @return 分页对象
    */
    @RequestMapping("/page")
    public Page page(@RequestParam Map<String, Object> params) {
        params.put(CommonConstant.DEL_FLAG, CommonConstant.STATUS_NORMAL);
        return sysRegionService.selectPage(new Query<>(params), new EntityWrapper<>());
    }

    /**
     * 添加
     * @param  sysRegion  实体
     * @return success/false
     */
    @PostMapping
    public R<Boolean> add(@RequestBody SysRegion sysRegion) {
        return new R<>(sysRegionService.insert(sysRegion));
    }

    /**
     * 删除
     * @param id ID
     * @return success/false
     */
    @DeleteMapping("/{id}")
    public R<Boolean> delete(@PathVariable Integer id) {
        SysRegion sysRegion = new SysRegion();
        sysRegion.setId(id);
        return new R<>(sysRegionService.updateById(sysRegion));
    }

    /**
     * 编辑
     * @param  sysRegion  实体
     * @return success/false
     */
    @PutMapping
    public R<Boolean> edit(@RequestBody SysRegion sysRegion) {
        return new R<>(sysRegionService.updateById(sysRegion));
    }
}
